A Container is an instance of an image running as a process. You can have many containers running off the same image. Each container is connected to a private virtual network (bridge network). Each network routes through a NAT firewall on a host IP.
https://docs.docker.com/engine/reference/commandline/container_run/
docker container run {image}
{image} - The image that the container will run.
This will pull an image from a repository, create a new container and run it.
As a more advanced example, use:
docker container run --publish {inPort:outPort} --detatch --name {container_name} {image}
This will go to the docker image repository (hub.docker.com) and find an image with the same name specified, pull it into your local machine, add it to a new container, run the container as a background process, and map it to the specified ports.
Example:
docker container run -p 80:80 -d nginx
This one command does several things:
https://docs.docker.com/engine/reference/commandline/container_ls/
docker container ls
This lists all currently running containers.
To show all containers on your system:
docker container ls --all
https://docs.docker.com/engine/reference/commandline/container_stop/
When you list the currently running containers on your system, you should get a back a table with each container having a CONTAINER ID as its first column. To stop a container, use the container stop command with just enough characters in the CONTAINER ID as to make it unique from any other container running on your system.
Example (Assume the container id for the one we wish to stop is 6903032478ce):
Alternatively, you can use the NAME of the container as an identifier to stop it.
docker container stop 690
Note: As an interesting fact: The names generated for each of the un-named containers you create in docker comes from a list of adjectives as well as sir names of notable hackers and scientists.
**docker stop** $(**docker** ps -aq)
https://docs.docker.com/engine/reference/commandline/container_start/
docker container start {container_id}
This will start an existing container on your system.
https://docs.docker.com/engine/reference/commandline/container_logs/
If you are running a service in the foreground (meaning that you didn’t specify the detach flag) you will automatically see longs so long as you don't cancel the command (ctrl+c). If you are running the service in the background however, you will need to specify a log command to see that containers logs:
docker container logs {container_id}
Just like stopping a container, you only need enough characters in the ID as to make it unique from any other running container.
https://docs.docker.com/engine/reference/commandline/container_top/
docker container top {container_id}
This will show you all of the running processes for a specific container.
https://docs.docker.com/engine/reference/commandline/container_rm/
docker container rm {container_id}
This will remove a specific container from your system. Note that you cannot remove running containers unless you use the flag "-f" which forces the container to shut down before being deleted.
Note: If you wish to do an action to more than one container, you are welcome to add additional container id's or name's at the end of the statement.
If you have too many containers to count, you can remove all non-running containers with the following command:
docker container rm $(docker container ls -a -q)
If you don't already have a running container, you can use the combination of two flags to get an interactive shell for that container.
docker container run -it {image} {shell}
Shell, in this case, specifies the type of shell you wish to simulate . In some images, the type of shell is assumed. For example, Ubuntu images will automatically assume that you are going to use the bash shell, so you would not need to specify "bash" after the image.
Since this is using the run command, it will create a new container upon executing the command. Once you exit the shell, the container will stop.
Instead of using the run command, you simply use the start command. This is especially useful if you make file changes within a container that you do not wish to lose. This, as well, uses the combination of two flags to get an interactive shell for that container.
docker container start -ai container_id
If a container is running, than it is already running all its specified processes. If you wish to open a terminal session for a currently running container, than you need to use the "exec" command:
docker container exec -it {container_id} {shell}
Exec is used to execute commands in a running container. The two flags for the "exec" command are the same for the "run" command. Exiting the shell when using the "exec" command will not stop the container since it is running an additional process on top of the container.
Note: In Alpine: you will also need to add --entrypoint "/bin/sh". This applies to both new containers and running containers.