Docker Compose tool is used to define and start running multi-container Docker applications. Configuration is as easy, there would be a 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 app’s environment.docker-compose.yml
for the services that make up your app services.docker-compose up
and Compose starts and runs your entire app.This quickstart assumes basic understanding of Docker concepts, please refer earlier posts for understanding on Docker & how to install and containerize applications.In this post,we can look at how to use existing Docker compose file and define services.
docker-compose up
runs, if it finds any containers from previous runs, it copies the volumes from the old container to the new container. This process ensures that any data you’ve created in volumes isn’t lost.In the next section,we can look at how define services in compose file for sample application with Angular as front end,Spring Boot as API and Postgres as Database.
Quick Snapshot
We have already defined below docker files in the previous posts here,here & here. Create new project directory and copy all dockerfiles to this folder.
FROM httpd:2.4 #copy angular dist folder to container COPY dist/ /usr/local/apache2/htdocs/ #copy htaccess and httpd.conf to container COPY .htaccess /usr/local/apache2/htdocs/ COPY httpd.conf /usr/local/apache2/conf/httpd.conf #change permissions RUN chmod -R 755 /usr/local/apache2/htdocs/ #expose port EXPOSE 4200
FROM openjdk:8-jre WORKDIR / #add required jars ADD spring-boot-rest-postgresql-0.0.1-SNAPSHOT.jar spring-boot-rest-postgresql-0.0.1-SNAPSHOT.jar #expose port EXPOSE 8080 #cmd to execute ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/spring-boot-rest-postgresql-0.0.1-SNAPSHOT.jar"]
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 dockerfiles ready for all of the application,next step is to define compose file.
Create a file called docker-compose.yml
in your project directory and paste the following:
version: '3' services: ui: build: context: . dockerfile: UIDockerfile ports: - '4200:4200' networks: - samplenet links: - 'api:api' api: build: context: . dockerfile: AppDockerfile ports: - '8080:8080' depends_on: - db - rabbitmq networks: - samplenet links: - 'db:db' db: 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 rabbitmq: image: 'rabbitmq:3.5.3-management' container_name: rabbitmq2 ports: - '5672:5672' - '15672:15672' networks: - samplenet networks: samplenet: null volumes: postgresdb: {}
Above compose file defines 4 services as below:
ui
: this is for angular application,it uses an image that’s built from the UIDockerfile in the current directory and forwards the exposed port 4200
on the container to port 4200
on the host machine.api
: this is for spring boot application,it uses an image that’s built from the AppDockerfile in the current directory and forwards the exposed port 8080
on the container to port 8080
on the host machine. Note this is dependent on db
and rabbitmq
services.db
: this is for postgresdb application,it uses an image that’s built from the DBDockerfile in the current directory and forwards the exposed port 5432
on the container to port 5432
on the host machine.rabbitmq
: Uses rabbitmq:3.5.3-management
public image from Dockerhub registry and uses ports 5672,15672
samplenet
network andpostgresdb
definition is for db
servicehealthcheck
section for db service to keep tab on the health of the databasedepends_on
denotes the service dependencies. When you start the services, compose would start the dependent services as well.In the current project directory, run docker-compose up
to start the application. Compose pulls all the required Docker image, builds an image for your code, and starts the services you defined.
You can stop the application, either by running docker-compose down
from within your project directory or by hitting CTRL+C in the original terminal where you started the app.
To apply patches/updates to any of application container(s) they can be stopped using below command
docker-compose stop <service name>
or if you want to force stop then use below command
docker-compose kill <service name>
To bring up service back online, use below command
docker-compose start <service name>
If you don’t want to compile dependencies, use below command
docker-compose up -d --no-deps <service name>
--no-deps
will not start linked services
-d
Detached mode
If there are existing containers for a service, and the service’s configuration or image was changed after the container’s creation, docker-compose up
command picks up the changes by stopping and recreating the containers.
Congrats! today we have learnt how to build and run your application using Docker compose utility.
Like this post? Don’t forget to share it!
Useful Resources:
There are few things as valuable to a business as well-designed software. Organizations today rely…
The cryptocurrency industry is being reshaped by the fusion of blockchain technology and artificial intelligence…
Introduction Artificial Intelligence (AI) has also found its relevance in graphic design and is quickly…
Imagine a world where the brilliance of Artificial Intelligence (AI) meets the unbreakable security of…
In today’s fast-paced digital landscape, automation is not just a luxury but a necessity for…
The world of casino gaming has leveraged the emerging technology advancements to create immersive and…
This website uses cookies.