This write-up will elaborate on:
- “docker run” Vs “docker create”
- How to Use “docker run” in Docker?
- How to Use “docker create” in Docker?
“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:
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:
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:
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:
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:
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:
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.