Introduction

Docker is a common containerization tool that is used to give software programs access to a filesystem that has all the components they require to function. Given the merciless consistency of its run-time environment, using Docker containers guarantees that the program will act consistently everywhere it is deployed.

We'll give a quick overview of the connection between Docker images and Docker containers in this article. Finally, we'll get into greater depth about running, starting, stopping, and removing containers.

A Docker image may be compared to a blank template that is used to build Docker containers. In contrast to a traditional Linux distribution, a Docker image often only includes what is absolutely necessary to execute the application. Images serve as the foundation for Docker containers.

The 'docker run' command brings images to life by building a container on top of the image and adding a read-write layer. A union file system is a collection of read-only layers coupled with read-write layers.

The docker run command generates a fresh container from the specified image each time you use it. Let's look at some examples to help clarify this as it might be confusing.

 

Prerequisites

  1. Ubuntu 20.04 installed or an Ubuntu 20.04 server set up.
  2. Docker installed and set up. (Note: click here to learn how to install and use Docker on ubuntu.)
  3. User account with admin privileges.
  4. Working knowledge of the Linux command line.

 

Employing Docker Containers

 

Step 1: The base Ubuntu image will be used to construct a new container when the following 'docker run' command is used. The flag '-t' provides us with a terminal and '-i' helps us to interact with the terminal.

docker run -ti ubuntu

The CLI changes and displays the 12 character container ID and the fact that the root user is currently logged in.

 

Step 2We will 'echo' some data into the container's /tmp directory, this will make a modification to the container.

Now we'll use the 'cat' command to ascertain the the changes were saved.

echo "Example1" > /tmp/Example1.txt
cat /tmp/Example1.txt

 

Step 3: Use the 'exit' command to exit the container.

exit

 

Step 4: Our container is terminated as soon as we leave the bash terminal, because Docker containers stop operating immediately as the command they issued is finished. We won't see ours if we use the 'docker ps' command, which only displays running containers.

docker ps

 

Step 5: Adding the '-a' flag to the 'docker ps' command displays all containers regardless of the fact if they are running or stopped, our container will be visible now.

docker ps -a

Whenever a container is created it is assigned a container ID and container name, both of which are randomly generated. The 'docker ps -a' command also displays additional information like the image used to create the container, the command that was ran on it, etc.

 

Step 6: Re-running the 'docker run -ti' command creates an completely new container.

docker run -ti ubuntu

We can confirm that it is a new container as a new container ID is displayed and when we try to locate our example file we won't find it. This might cause some confusion but there is no reason to worry, we will now exit the new container and see that both the containers exist.

 

Step 7: Utilize the 'docker start' command along with '-a' and '-i' flags to restart a pre-existing container and make it interactive, then add the container ID of the container you wish to open after the flags.

Now we use the 'cat' command and find that the file we created earlier still exists.

docker start -ai <Container ID>
cat /tmp/Example1.txt

 

Step 8: Exit the container

exit

 

Step 9: Now to complete this quick tutorial we will delete both of the containers that we created using the 'docker rm' command followed by the container ID or the Container name.

Note: 'docker rm' command works only on stopped containers.

docker rm <Container1 ID/Container1 Name> <Container2 ID/Container2 Name>

 

Conclusion

We learned how to create a container and how the 'docker run' command creates a completely new container each time we run it. We also learned how to locate a stopped container, connect to it and stop it and finally we learned how to remove pre-existing containers.

 

 

Var dette svaret til hjelp? 2 brukere syntes dette svaret var til hjelp (4 Stemmer)