Docker as we know, is an open platform for developers and sysadmins to build, ship, and run distributed applications, whether on laptops, data center VMs, or the cloud. In this post, we are going to take look at how to build a Docker image for Database application.
Quick Snapshot
OK, now we have got the docker setup, the next step is to define the docker container.
# Dockerfile FROM postgres:9.5 MAINTAINER Author Name author@email.com
#init.sql to create database COPY init.sql /docker-entrypoint-initdb.d/
# Adjust PostgreSQL configuration to accept remote connections RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf RUN echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf
init.sql
that would have statements to create the database like the one below CREATE DATABASE testdb;
#expose port EXPOSE 5372
FROM postgres:9.5 #init.sql to create database COPY init.sql /docker-entrypoint-initdb.d/ # Adjust PostgreSQL configuration to accept remote connections RUN echo "host all all 0.0.0.0/0 md5" >> /var/lib/postgresql/data/pg_hba.conf RUN echo "listen_addresses='*'" >> /var/lib/postgresql/data/postgresql.conf # expose port EXPOSE 5432
Now that we have completed Dockerfile, next step is to build Docker image by docker build command
docker build -f DBDockerfile -t postgresdb .
Here -t
specifies the name of the image and -f
specifies the name of the Dockerfile. Image tag I have left it empty.
Congrats! You’ve successfully built a container for your Database application.
To run the Docker image, execute the following commands
docker run -p 5372:5372 postgresdb
Here -p
specifies the port container: host mapping.
Congrats! You’ve successfully built and run container for your Database application.
If you’re looking for running multi-container applications using Docker Compose tool then the configuration is as easy, there would be YAML file to configure your application’s services/networks/volumes, etc., Then, with a single command, you can create and start all the services from the compose configuration.
Here are the key steps :
Dockerfile
for your container(s).docker-compose.yml
for the services that make up your application services.docker-compose up
and Compose starts and runs your entire app.Sample docker-compose.yml
for Angular UI application would look like this:
version: '3' services: postgresdb: build: context: . dockerfile: DBDockerfile volumes: - 'postgresdb:/var/lib/postgresql/data' environment: POSTGRES_USER: postgres POSTGRES_PASSWORD: postgres POSTGRES_DB: testdb ports: - '5432:5432' healthcheck: test: - CMD-SHELL - 'pg_isready -U postgres' interval: 10s timeout: 5s retries: 5 networks: - samplenet networks: samplenet: null volumes: postgresdb: {}
For database services, volumes need to be mounted so that data can be persisted. Also, you can have healthcheck section to check the health of the database at regular intervals.
To scale services using Docker compose refer here. There is much more to the Docker platform than what was covered here, but now you would have got a good idea of the basics of building containers for an application.
Like this post? Don’t forget to share it!
Operating a business often entails balancing tight schedules, evolving market dynamics, and shifting consumer requirements.…
Of course, every site has different needs. In the end, however, there is one aspect…
In today's digital-first world, businesses must adopt effective strategies to stay competitive. Social media marketing…
62% of UX designers now use AI to enhance their workflows. Artificial intelligence (AI) rapidly…
The integration of artificial intelligence into graphic design through tools like Adobe Photoshop can save…
The cryptocurrency trading world has grown significantly in recent years, with automation playing a key…
This website uses cookies.