docker runcommand. In this section, you'll be learning about container manipulation in a lot more detail. Container manipulation is one of the most common task you'll be performing every single day so having a proper understanding of the various commands is crucial.
docker runto create and start a container using the
hello-worldimage. The generic syntax for this command is as follows:
dockerdaemon. Prior to version
1.13, Docker had only the previously mentioned command syntax. Later on, the command-line was restructured to have the following syntax:
object-typeindicates the type of Docker object you'll be manipulating. This can be a
commandindicates the task to be carried out by the daemon i.e.
optionscan be any valid parameter that can override the default behavior of the command i.e. the
--publishoption for port mapping.
runcommand can be written as follows:
image namecan be of any image from an online registry or your local system. As an example, you can try to run a container using the fhsinchy/hello-dock image. This image contains a simple Vue.js application that runs on port 80 inside the container. To run a container using this image, execute following command on your terminal:
--publish 8080:80portion which will be explained in the next sub-section.
-poption is as follows:
--publish 8080:80in the previous sub-section, it meant any request sent to port 8080 of your host system will be forwarded to port 80 inside the container.
ctrl + ckey combination while the terminal window is in focus or closing off the terminal window completely.
runcommand is the
-doption. In the example above, in order for the container to keep running, you had to keep the terminal window open. Closing the terminal window also stopped the running container.
--detachoption to the
runcommand as follows:
--publishoption before the
--detachoption, it'll work just the same. One thing that you have to keep in mind in case of the
runcommand is that the image name must come last. If you put anything after the image name then that'll be passed as an argument to the container entry-point (explained in the Executing Commands Inside a Container sub-section) and may result in unexpected situations.
container lscommand can be used to list out containers that are currently running. To do so execute following command:
gifted_sammetis running. It was created
5 seconds agoand the status is the status is
Up 5 seconds,which indicates that the container is running fine since it's creation.
9f21cb777058which is the first 12 characters of the full container ID. The full container ID is
9f21cb77705810797c4b847dbd330d9c732ffddba14fb435470567a7a3f46cdcwhich is 64 characters long. This full container ID was printed as the output of the
docker container runcommand in the previous section.
PORTScolumn, port 8080 from your local network is pointing towards port 80 inside the container. The name
gifted_sammetis generated by Docker and can be something completely different in your computer.
container lscommand only lists the containers that are currently running on your system. In order to list out the containers that has run in the past you can use the
reverent_torvaldswas created earlier and has exited with the status code 0, which indicates that no error was produced during the runtime of the container.
CONTAINER ID- a random 64 characters long string.
NAME- combination of two random words, joined with an underscore.
--nameoption. To run another container using the
fhsinchy/hello-dockimage with the name
hello-dock-containeryou can execute the following command:
gifted_sammetcontainer (the container created in the previous sub-section), that's why you'll have to use a different port number i.e. 8888. Now to verify, run the
hello-dock-containerhas been started.
container renamecommand. Syntax for the command is as follows:
hello-dock-container-2execute following command:
container lscommand. The
renamecommand works for containers both in running state and stopped state.
ctrl + ckey combination. Containers running in the background, however, can not be stopped in the same way.
container stopcommand. Generic syntax for the command is as follows:
container identifiercan either be the id or the name of the container. I hope that you remember the container you started in the previous section. It's still running in the background. Get the identifier for that container using
docker container ls, I'll be using
hello-dock-containercontainer for this demo. Now execute following command to stop the container:
stopcommand shuts down a container gracefully by sending a
SIGTERMsignal. If the container doesn't stop within a certain period, a
SIGKILLsignal is sent which shuts down the container immediately.
SIGKILLsignal instead of a
SIGTERMsignal, you may use the
container killcommand instead. The
container killcommand follows the same syntax as the
container startcommand can be used to start any stopped or killed container. The syntax of the command is as follows:
container ls --allcommand. Then look for the containers with
hello-dock-containercontainer, you may execute the following command:
container lscommand. The
container startcommand starts any container in detached mode by default and retains any port configurations made previously. So if you visit
http://127.0.0.1:8080now, you should be able to access the
hello-dockapplication just like before.
container restartcommand. The
container restartcommand follows the exact syntax as the
container restartcommand attempts to stop the target container and then starts it back whereas the start command just starts a already stopped container.
container runcommand which is in reality a combination of two separate commands. These commands are as follows:
container createcommand creates a container from a given image.
container startcommand starts a container that has been already created.
container ls --allcommand, a container with the name of
hello-dockhas been created using the
STATUSof the container is
Createdat the moment and given it's not running, it won't be listed without the use of the
--alloption. Once the container has been created, it can be started using the
STATUShas changed from
Up 29 secondswhich indicates that the container is now in running state. The port configuration has also showed up in the
PORTScolumn which was previously empty.
container runcommand for majority of the scenarios, there will be some situations later on in the article that requires the usage of this
container rmcommand. The generic syntax is as follows:
container ls --allcommand and look for containers with
128ec8ceab71is not running. To remove the
6cf52771dde1you can execute the following command:
container lscommand. You can also remove multiple containers at once by passing their identifiers one after another separated by spaces.
-foption to skip this confirmation step. Once done, the
container prunecommand will show the amount of reclaimed space.
container ls --allcommand to make sure that the dangling containers have been removed:
hello-dock-container-2in the list. I would suggest stopping and removing both containers before going to the next section.
--rmoption for the
container startcommands which indicates that you want the containers removed as soon as they're stopped. To start another
hello-dockcontainer with the
--rmoption, execute the following command:
container lscommand to verify that the container is running:
container ls --allcommand:
--rmoption for most of the containers. I'll explicitly mention where it's not needed.
bashand in case of the programming languages or run-times, it is usually their default language shell.
-itoption to be passed in the
ubuntuimage by executing
docker container run ubuntuyou'll see nothing happens. But if you execute the same command with
-itoption, you should land directly on bash inside the Ubuntu container.
cat /etc/os-releasecommand, I am indeed interacting with the bash running inside the Ubuntu container.
-itoption sets the stage for you to interact with any interactive program inside a container. This option is actually two separate options mashed together.
--interactiveoption connects you to the input stream of the container, so that you can send inputs to bash.
--ttyoption makes sure that you get some good formatting and a native terminal like experience by allocating a pseudo-tty.
-itoption whenever you want to run a container in interactive mode. Another example can be running the
nodeimage as follows:
-ityou can instead be more verbose by writing
uname -acommand inside an Alpine Linux container. Scenarios like this where all you want is to execute a certain command inside a certain container is pretty common.
base64program which is something available in almost any Linux or Unix based operating system but not on Windows. In this situation you can quickly spin up a container using images like busybox and let it do the job.
base64is as follows:
container runcommand, whatever you pass after the image name gets passed to the default entry-point of the image. An entrypoint is like a gateway to the image. Most of the images except the executable images (explained in the Working With Executable Images sub-section), uses shell or
shas the default entry-point. So any valid shell command can be passed to them as arguments.
touchcommand to do so. Now, I have a directory on my computer with following files:
rmbyextprogram running inside the container doesn't have any access to your local file system. So, if somehow you can map the local directory containing the
/zonedirectory inside the container, the files should be accessible to the container.
-v $(pwd):/zonepart in the command,
--volumeoption is used for creating a bind mount for a container. This option can take three fields separated by colons (
:). The generic syntax for the option is as follows:
/home/fhsinchy/the-zoneand given my terminal is opened inside the directory,
$(pwd)will be replaced with
/home/fhsinchy/the-zonewhich contains the previously mentioned
.txtfiles. You can learn more about command substitution if you want to.
-voption is valid for the
container runas well as the
container createcommands. Volumes will be explored in greater detail in the upcoming sections so don't worry if you didn't understand it very well here.
sh, in this case the
rmbyextprogram and as you've learned in the previous sub-section, anything you write after the image name in a
container runcommand gets passed to the entry-point of the image.
docker container run --rm -v $(pwd):/zone fhsinchy/rmbyext pdfcommand translates to
rmbyext pdfinside the container. Executable images are not that common in the wild but can be very useful in certain cases, hence learning about them is important.