Part 5: Deployment of Container through Docker Compose File

In this part, we will look at an interesting example to deploy multiple containers just by docker compose file.

We don’t need to do anything just need to write our compose file and that too from the Docker HUB.

Lets see an example.

1. Create docker-compose.yml file

$ mkdir compose_example1
$ cd compose_example1/
$ ls -ltr

$ cat docker-compose.yml
version: '3'
   image: nginx
    - 5003:80
   image: redis

### Verify if everything if fine
### Using config option
$ docker-compose config
    image: redis
    - 5003:80
    image: nginx
version: '3.0'

2. Build and run the docker-compose file configuration.

### Create and start the containers.
$ docker-compose up -d
Creating network "compose_example1_default" with the default driver
Pulling web (nginx:)...
latest: Pulling from library/nginx
177e7ef0df69: Already exists
ea57c53235df: Pull complete
bbdb1fbd4a86: Pull complete
Pulling database (redis:)...
latest: Pulling from library/redis
177e7ef0df69: Already exists
66ec699db42d: Pull complete
9af6d87fd347: Pull complete
de9172cdb09c: Pull complete
27733a222e28: Pull complete
ef1ae1903ba4: Pull complete
Creating compose_example1_web_1      ... done
Creating compose_example1_database_1 ... done

$ docker container ls
CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS              PORTS               NAMES
f986b22a7ce6        nginx               "nginx -g 'daemon of…"   2 minutes ago       Up 2 minutes        80/tcp              compose_example1_webserver_1
be3a1d0ea17f        redis               "docker-entrypoint.s…"   2 minutes ago       Up 2 minutes        6379/tcp            compose_example1_database_1

### Stop the container services
$ docker-compose down
Stopping compose_example1_webserver_1      ... done
Stopping compose_example1_database_1 ... done
Removing compose_example1_webserver_1      ... done
Removing compose_example1_database_1 ... done
Removing network compose_example1_default

$ docker container ls
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

3. Check this out in browser.

### If you open the post 5003 which is forwarded in docker compose file,
### you can see the nginx server output.

To scale a container service to replicate it, we can use scale option:
$ docker-compose up -d –scale database=2
Creating network “compose_example1_default” with the default driver
Creating compose_example1_database_1 … done
Creating compose_example1_database_2 … done
Creating compose_example1_web_1 … done
0 0 votes
Article Rating
Notify of

Inline Feedbacks
View all comments