Docker

Difference Between Docker Run and Create

The Docker tool is available in both Docker GUI and Docker CLI versions. However, Docker CLI provides greater control and flexibility in managing Docker components. For this purpose, Docker CLI offers a wide range of command line utilities, including Docker run and Docker create commands, which support different ways to create and start containers.

This write-up will elaborate on:

“docker run” Vs “docker create”

The “docker run” and “docker create” both are utilized to build the Docker containers using the container’s image or snapshot created by the “docker build” command. The primary difference between these two commands is that the “docker run” command creates the container and automatically starts it. In contrast, the “docker create” only creates the Docker container but does not start it automatically. The container created by the “docker create” command can be started through the “docker start” command.

How to Use “docker run” in Docker?

The “docker run” command creates the container from a snapshot or image and starts it automatically. Follow the provided instructions to utilize the “docker run” command.

Step 1: Create Program File

First, create a file named “index.html” file and paste the below given HTML code into the file:

<html>

<head>

<style>

body{

background-color:rgb(9, 4, 4);

}

h1{

color:rgb(221, 219, 226);

font-style: italic;

}

</style>

</head>

<body>

<h1> This is First HTML page </h1>

</body>

</html>

Step 2: Make Dockerfile

Next, make another file named “Dockerfile” and add the following instructions to dockerize the HTML program:

  • FROM” instruction allocates the base image to the container.
  • COPY” instruction sends the source file to the container path.
  • ENTRYPOINT” specifies the container’s executing point as starting point or container’s executables:
FROM nginx:latest

COPY index.html /usr/share/nginx/html/index.html

ENTRYPOINT ["nginx", "-g", "daemon off;"]

Step 3: Make Docker Image

After that, create the container image or snapshot by utilizing the below command. Here, the image name is defined through the “-t” tag:

docker build -t html:latest .

Step 4: Create and Run Container

Utilize the “docker run” command to create the container from the snapshot and run it automatically. Here:

  • -p” is utilized to define the exposing port of the container.
  • –name” specifies the container’s name.
  • -d” executes the container in detached mode:
docker run --name html1-container -d -p 80:80 html:latest

Now, visit the localhost and check if the container is started or not:

How to Use “docker create” in Docker?

The “docker create” command only creates the container, and these containers are then started through the “docker start” command. For the illustration, utilize the given steps.

Step 1: Create Container

First, create the container from the container’s snapshot created by the “docker build” command in the previous section. For this purpose, use the “docker create –name <cont-name> -p <port> <image-name>” command:

docker create --name new-html-cont -p 80:80 html:latest

Step 2: Start Container

Next, start the container through the “docker start <cont-name>” command. Here, you can use the container’s id as well to start the container:

docker start new-html-cont

After that, verify if the container is started or not by navigating to the assigned port of the localhost. The output shows that we have started the container successfully:

We have demonstrated the key difference between the “docker run” and “docker create” commands and how to utilize them for creating containers.

Conclusion

The commands “docker run” and “docker create” are used to build Docker containers using the container’s image or snapshot created by the “docker build” command. The main distinction between these two commands is that the “docker run” command generates and starts the container, whereas the “docker create” command only generates or creates the container but does not start it automatically. This article has explained the primary distinction between Docker “run” and “create” commands.

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.