Docker

Different Ways to Use “docker-compose up” Command

Docker compose is the most essential and crucial part of the Docker environment. It is referred to as a multi-container tool used to fire up the application and other microservices in multiple containers. Docker Compose utilizes the YAML file to configure the applications and other configuration settings. Then, the application is containerized in multiple containers by utilizing the “docker-compose up” command.

This blog will demonstrate different ways to use the “docker-compose up” command.

Different Ways to Use the “docker-compose up” Command

The “docker-compose up” command is used to run the applications and programs in multiple containers. These commands support different options to function differently. For the demonstration, we have listed some methods to use the “docker-compose up” command:

Method 1: Use the “docker-compose up” Command to Containerize Multi Container Program

To containerize the multiple container service or application, first, create the configuration files, such as “Dockerfile” and “docker-compose.yml” file. Then, utilize the “docker-compose up” command. For the proper guideline, go through the provided steps.

Step 1: Make Dockerfile

First, create the Dockerfile that includes the instructions to dockerize the application. For instance, we will containerize the “index.html” file:

FROM nginx:latest
COPY index.html /usr/share/nginx/html/index.html
ENTRYPOINT ["nginx", "-g", "daemon off;"]

 

Step 2: Create Compose File

Next, add the services configuration setting into the “docker-compose.yml” file. To do so, we have configured the following settings:

  • services” configures two services, “web” and “web1”.
  • build” is used to specify the build context. For instance, we have used Dockerfile. Here, you can also utilize “image” to containerize the program or application.
  • ports” allocates the container’s exposed port:
version: "3"
services:
  web:
    build: .
    ports:
      - 80:80
  web1:
   build: .
   ports:
     - 80

 

Step 3: Start the Container

Next, create and start the container by utilizing the “docker-compose up” command:

docker-compose up

 

To check if the application service is running in the container or not, visit the exposed local host port:

Method 2: Use the “docker-compose up” Command to Run Container in Detached Mode

In order to run the compose services in the background or detached mode, utilize the “-d” or “–detached” option along with the “docker-compose up” command:

docker-compose up -d

 

Above output shows that containers are executing in detached mode.

Method 3: Use the “docker-compose up” Command to Start Container Without Re-creating Them

To prevent Docker compose from recreating the container before start it, utilize the “–no-recreate” option with the following command:

docker-compose up -d --no-recreate

 

Method 4: Use the “docker-compose up” Command to Create the Containers Only

Sometimes, developers may want to create or configure services in containers while preventing Docker compose from starting them. For this purpose, simply use the “–no-start” flag with the “docker-compose up”:

docker-compose up --no-start

 

Method 5: Use the “docker-compose up” Command to Pull Image Before Starting the Container

Some Docker compose services use Docker images rather than build context or Dockerfile. In scenarios, you can first pull the Docker image from the registry before creating and starting the container using the “–quiet-pull” or “–pull” option:

docker-compose up --quiet-pull

 

We have discussed the different ways to use the “docker-compose up” command.

Conclusion

The “docker-compose up” command configures and runs multi-container applications and services. It supports different options to behave differently, such as “-d” is used to run service in detached mode, “–no-recreate” prevents the Docker engine from recreating the container, “–pull” pulls the image first before containerizing the app or service. This blog has illustrated different ways to use the “docker-compose up” command.

About the author

Rafia Zafar

I am graduated in computer science. I am a junior technical author here and passionate about Programming and learning new technologies. I have worked in JAVA, HTML 5, CSS3, Bootstrap, and PHP.