This write-up will demonstrate:
- Difference Between “docker-run” and “docker-compose” in Docker
- How to Use “docker run” in Docker?
- How to Use “docker-compose” in Docker?
Difference Between “docker run” and “docker-compose” in Docker
Both “docker run” and “docker-compose” commands are used for a similar purpose to build and fire up the containers to containerize the application or services. The primary difference between these two commands is the “docker run” command executes the image to create a single container, and it is a purely terminal-based command. While the “docker-compose” command reads instructions from the “docker-compose.yml” file and is used to manage and run multi-container applications. In other words, it can create and execute more than one container at a time.
How to Use “docker run” in Docker?
Docker “run” command can generate only a single container at one time. Look at the instructions to utilize the Docker run container to dockerize an application.
Step 1: Make Dockerfile
First, create a simple Dockerfile that contains simple instructions to dockerize the application. For instance, we have created “main1.dockerfile” to containerize the “main1.go” Golang code:
COPY main1.go .
RUN go build -o webserver .
Step 2: Create an Image
Generate a new image by utilizing the Dockerfile. Here, “-t” specifies the image name, and “-f” is used to define the Dockerfile name or path:
Step 3: Create and Start Container
Next, utilize the “docker run” command to containerize the application using the Docker image. The “-d” flag will execute the container in detached mode, and “-p” specifies the local host port to expose the container:
List all the containers to check if the container is created and executing on exposing port or not:
Alternatively, you can visit the assigned port of the local host to check if the container is running or not:
How to Use “docker-compose” in Docker?
Docker compose is a core part of the Docker solution utilized to operate and manage multi containers projects and services. The “docker-compose” command reads the instructions from the compose file. Then, start the services by building and executing the containers.
To utilize the “docker-compose” in Docker, go through the given procedure.
Step 1: Create “docker-compose.yml” File
First, create a file named “docker-compose.yml” file and configure the services required to execute. For instance, we have configured the following configurations:
- “services” configures two different services, “web” and “web2”.
- The “web” service uses the “main.dockerfile” to containerize and run the “main.go” program.
- The “web2” service uses the “main1.dockerfile” to dockerize and execute the “main1.go” program.
- “ports” key is utilized to specify the exposing port for the container. In our scenario, Docker will automatically assign the exposing port to the “web” service, and the “web2” service will expose on port 8080:
Step 2: Start Containers
Next, start up the containers with the help of mentioned command:
Step 3: List the Containers
List down the compose containers and verify whether defined services are executing in separate containers or not:
Here, you can see “web” is executing on port “62689” and “web2” is exposing on “8080”:
You can navigate the exposing ports of containers for verification:
It can be observed that we have successfully executed two different containers at a time from the “docker-compose” command:
The “docker-compose” and “docker run” both commands are executed to create and run the containers. The key difference between these two commands is the “docker run” command creates a single container at one time. However, “docker-compose” can create, manage and run multiple containers simultaneously. This write-up has demonstrated the primary difference between the “docker-compose” and “docker run” commands.