Docker containers are now one of the most emerging technologies in a day. Docker containers are commonly used on CI / CD (Continuous Integration / Continuous Deployment) platforms. Containers are lightweight VMs (virtual machines) that are used under hypervisor resources (such as RAM, CPU, HDD, and kernel).
When you run the command against the image you basically get the container. After the command is executed at the end of the container, the container will be closed (you will get the container without moving or exiting). If you re-run another command in the same image, then a new container is created.
All the containers created will remain on the host filesystem until you choose to delete them by using the docker rm
command
In order to create and run a container, you need to run command into a downloaded image, in this case, Ubuntu, so a basic command would be to display the distribution version file inside the container us Cat Command, as in the following example:
The above command is divided as follows:
To run one of the containers again with the command that was executed to create it, first you must get the container ID (or the name automatically generated by Docker) by issuing the below command, which displays a list of the running and stopped (non-running) containers:
Once the container ID has been obtained, you can start the container again with the command that was used to create it, by issuing the following command:
Here, the string c758c33cf44d represents the container ID.
In case the container is running state, you can get it’s ID by issuing docker ps
command. To stop the running container issue docker stop
command by specifying the container ID or auto-generated name.
A more elegant alternative so you don’t have to remember the container ID would be to allocate a unique name for every container you create by using the --name
option on command line, as in the following example:
Then, using the name that you allocated for the container, you can manipulate container (start, stop, remove, top, stats) further just by addressing its name, as in the below examples:
Be aware that some of the above commands might display no output if the process of command that was used to create the container finishes. When the process that runs inside the container finishes, the container stops
Run an Interactive Session into a Container
In order to interactively connect into a container shell session, and run commands as you do on any other Linux session, issue the following command:
The above command is divided as follows:
-i
is used to start an interactive session.- -t allocates a tty and attaches stdin and stdout.
ubuntu
is the image that we used to create the container.bash
(or /bin/bash) is the command that we are running inside the Ubuntu container.
To quit and return to host from the running container session you must type exit
command. The exit command terminates all the container processes and stops it.
If you’re interactively logged on container terminal prompt and you need to keep the container in running state but exit from the interactive session, you can quit the console and return to host terminal by pressing Ctrl+p
and Ctrl+q
keys.
To reconnect to the running container you need the container ID or name. Issue docker ps
command to get the ID or name and, then, run docker attach
command by specifying container ID or name, as illustrated in the image above:
To stop a running container from the host session issue the following command:
This is for basic container manipulation. In the next tutorial, we will discuss how to save, remove, and run a web server in the docker container.
No responses yet