Play with docker and get familiar with various container commands.
Play with docker and get familiar with various container commands.
Working with containers is not just limited to starting and stopping the containers. It has much more features than just an OS.
What if you want to share the filesystem with the container? Does containers support that?
What are the parameters supported by docker command? Let's check it out.
docker --help
It lists down multiple options. Out of which few important options are the ones we will discuss further.
We can group these commands in three categories -
Container management
docker build - we have already discussed this option while building the images, so we will not go through it again.
docker ps / docker ps -a - This command lists all running / stopped containers in the workspace
docker rm - this command is used to remove a container from workspace. This is as good as VM termination.
docker rmi - this command is used to remove the underlying image from cache.
Docker images -- ?
Container execution
docker run - This command is used to run a container. It comes along with multiple options, which we will discuss in further sections. Just to summarise, the common options used are
volume
port
name
attach
rm
The whole list can be found with [docker run --help](https://docs.docker.com/engine/reference/commandline/run/)
docker stop - This will stop the running container.
docker start - This command will start the stopped container.
Container Communication
docker exec - This command will connect to running container and execute a given command on the container.
docker cp - This command is used to copy the files to/from the running container.
In this Crio Byte,You will go through these commands and get more familiar with containers.
Here are a list of things you will learn in this Crio Byte:
Play with containers.
Understand different commands used to manage containers..
Publish, delete, republish and understand the infrastructure .
Play with docker and get familiar with various container commands.
Working with containers is not just limited to starting and stopping the containers. It has much more features than just an OS.
What if you want to share the filesystem with the container? Does containers support that?
What are the parameters supported by docker command? Let's check it out.
docker --help
It lists down multiple options. Out of which few important options are the ones we will discuss further.
We can group these commands in three categories -
Container management
docker build - we have already discussed this option while building the images, so we will not go through it again.
docker ps / docker ps -a - This command lists all running / stopped containers in the workspace
docker rm - this command is used to remove a container from workspace. This is as good as VM termination.
docker rmi - this command is used to remove the underlying image from cache.
Docker images -- ?
Container execution
docker run - This command is used to run a container. It comes along with multiple options, which we will discuss in further sections. Just to summarise, the common options used are
volume
port
name
attach
rm
The whole list can be found with [docker run --help](https://docs.docker.com/engine/reference/commandline/run/)
docker stop - This will stop the running container.
docker start - This command will start the stopped container.
Container Communication
docker exec - This command will connect to running container and execute a given command on the container.
docker cp - This command is used to copy the files to/from the running container.
In this Crio Byte,You will go through these commands and get more familiar with containers.
Here are a list of things you will learn in this Crio Byte:
Play with containers.
Understand different commands used to manage containers..
Publish, delete, republish and understand the infrastructure .
If you are taking this Byte on your personal Linux machine, open a new terminal and proceed to the next milestone. You may skip the rest of this milestone.
To open a terminal in the Crio Workspace, follow the steps below:
Click on the workspace icon on the left sidebar and click on the Start
button to create your workspace (Note: it can take up to 15 minutes to create your workspace).
Once your workspace is ready, click on Launch online IDE
.
Click on View > Open in New Tab
option (see image below).
Menu > View > Terminal
.
(Note: The menu button is on the top-left (three horizontal bars).If you remember, we had created a container apache-server
using Dockerfile. Lets try to run it again.
docker run -p80:80 apache-server
This will leave the container running. Now, let's open another terminal and check if the container is running. To do that, issue this command in another terminal.
docker ps
Do you see the output?
Here, we first tried to list all running containers, if your container is not listed here, which means it's not running. Check if it appears in stopped containers.
docker ps -a
Which lists all containers including stopped one’s
Is there a way to start this stopped container again?
docker start <container_id>
Note that this container id is the one picked up from ps.
Now, lets confirm tha the container is running
docker ps
Do you remember, when we initially ran the container, we also mapped ports? Do you remember how we mapped them?
-p80:80
This mapped guest port 80 with host port 80. If I am not wrong, this should still be active. Lets try that out?
curl localhost
So, an important point to note here is, when you run the container with the docker run
command, a VM and its hardware association is created. You cannot change it later.
So it is very important to decide port binding and volumes upfront, when you run the container for the first time.
Now, let’s try to run the apache container from our host content.
Lets first run below command to stop and remove the running container
docker stop container_id
docker rm container_id
docker ps -a
Shows no containers now.
Now let's modify our Dockerfile to remove the contents of apache server
I have commented the commands that wrote some content into /usr/local/apache2/htdocs
which is the base directory for an apache server.
docker build -t apache-server .
Let's try to run it now.
First, try to check is the image is tagged correctly.
docker images
docker run -p80:80 apache-server
This command by default attaches to the terminal. So leave it running and we will open a separate terminal to play around.
Open a separate terminal and run this command
curl localhost
You will see the default homepage of apache server..
In the previous milestone, we saw some text being returned when we executed curl localhost
from the command line.
Lets see from where it is coming.
The best way to get into any runnin container is to use exec
command. So lets try it.
docker ps
Note the container id.
Lets run docker exec command
docker exec <container_id> bash
Nothing happened. Why? Looks like we messup with some parameters. Letscheck the parameters
docker exec --help
So, the command we issued was actually executed, but nothing reported back. Because we missed few parameters.,
Here,
-d is used to run non-interactive mode
-e can be used to send environment variables.
-i is used to run in interactive mode, which means it will keep the std io connected.
-t is used to allocate a terminal to the running process.
In our case, we need -i
-t
in order to run it in an interactive terminal.
Lets try it again
You can see the terminal opened for bash process. Lets tryt to play around by using ls, cd cat commands
By the way, in these commands, you would have seen that the content of index.html is exactly the same as what we saw without curl localhost
command.
Now let's try to change these contents.
First exit from the terminal inside container.,
Then lets try to create a simple file on our host machine.
echo "This is my simple html file" > ~/index.html
cat ~/index.html
Now, let's copy this file into docker container.
Syntax is
docker cp -h <remote_host> <source_path> <container_id>:<destination_path>
Here, -h
means remote host. This parameter is available across all docker commands, when you want to execute the, on a remote docker host.
Lets run this command now. We will ignore the -h
option as we are running on localhost only. Default docker host is localhost.
docker cp ~/index.html 958c3c6d7e1e:/usr/local/apache2/htdocs/index.html
Now lets connect to the docker again to see if the contents are changed.
docker exec -it 958c3c6d7e1e bash
cat htdocs/index.html
Does it reflect on the apache server on the container which is attached to port 80?
curl localhost
Now, lets stop this container
docker ps
docker stop upbeat_wescoff
Do you know what this upbeat_wescoff
is? Its container name. When you run a container, docker runtime gives it a cool name, if not already provided
docker run --help | grep name
So you can use --name parameter while starting the container.
Lets try this option with port 8080 instead of 80.
Before that, lets go to previous bash screen to confirm that the container is stopped.
So we are ready to start the container again.
Lets first create a file on our host system
echo "This file is hosted from host system" > index.html
mkdir html
mv index.html html
cat html/index.html
Confirm that the file is created and contents are there as expected.
Now start the container
docker run -p8080:80 --name my-apache-server -v html:/usr/local/apache2/htdocs apache-server
Note that we have three changes in the command
-p how used 8080 instead of 80, which means guest 80 port is now mapped with 8080 port on the host.
--name my-apache-server - This is the name we are assigning to our container.
-v html:/usr/local/apache2/htdocs - We are mounting a local volume on the apache server, so that the we can share the file system.
Lets see the results one by one.
docker ps
curl localhost:8080
Now that we are done with playing around with docker, lts now try to stop the container.
docker stop -my-apache-server
Lets see how many images are there now.
docker images -a
Lets try to remove an image
docker rmi apache-server
docker images -a
Will show a bunch of images which are used and unused.
Lets try to delete unused images
docker image prune
We still have few images there.. Let's try to clean up everything.
docker system prune
This command will remove all unused containers and dangling images.
Consider a Dockerfile like this
FROM gradle:jdk11
ENV DEBIAN_FRONTEND=noninteractive
RUN apt-get update && apt-get -y upgrade
RUN apt-get -y install git redis-server wget
git clone [git@gitlab.crio.do](mailto:git@gitlab.crio.do):public_content/spring-starter.git
cd spring-starter
chmod +x /gradlew
./gradlew bootkar
The question is, suppose you created a new commit in spring-starter repository, and executed
docker build -t spring-starter .
Will it pick up the latest version of spring-starter repository? If yes, why? If no, why not?
Working with containers is not just limited to starting and stopping the containers. It has much more features than just an OS.
What if you want to share the filesystem with the container? Does containers support that?
What are the parameters supported by docker command? Let's check it out.
run > docker --help
It lists down multiple options. Out of which few important options are the one’s we will discuss further.
We can group these commands in three categories -
Container management
docker build - we have already discussed this option while building the images, so we will not go through it again.
docker ps / docker ps -a - This command lists all running / stopped containers in the workspace
docker rm - this command is used to remove a container from workspace. This is as good as VM termination.
docker rmi - this command is used to remove the underlying image from cache.
Docker images -- ?
Container execution
docker run - This command is used to run a container. It comes along with multiple options, which we will discuss in further sections. Just to summarise, the common options used are
volume
port
name
attach
rm
The whole list can be found with [docker run --help](https://docs.docker.com/engine/reference/commandline/run/)
docker stop - This will stop the running container.
docker start - This command will start the stopped container.
Container Communication
docker exec - This command will connect to running container and execute a given command on the container.
docker cp - This command is used to copy the files to/from the running container.
We will see these commands in advance docker byte..