Byte Introduction

You will take a deep dive into container management commands, learn about layers and play with docker containers on your machine

Skills:

Linux Commands

Objective

Play with docker and get familiar with various container commands.

Background

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

  1. docker build - we have already discussed this option while building the images, so we will not go through it again.

  2. docker ps / docker ps -a - This command lists all running / stopped containers in the workspace

  3. docker rm - this command is used to remove a container from workspace. This is as good as VM termination.

  4. docker rmi - this command is used to remove the underlying image from cache.

  5. Docker images -- ?

Container execution

  1. 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

    1. volume

    2. port

    3. name

    4. attach

    5. rm

    6. The whole list can be found with [docker run --help](https://docs.docker.com/engine/reference/commandline/run/)

  2. docker stop - This will stop the running container.

  3. docker start - This command will start the stopped container.

Container Communication

  1. docker exec - This command will connect to running container and execute a given command on the container.

  2. 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.

Primary goals

Here are a list of things you will learn in this Crio Byte:

  1. Play with containers.

  2. Understand different commands used to manage containers..

  3. Publish, delete, republish and understand the infrastructure .

Objective

Play with docker and get familiar with various container commands.

Background

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

  1. docker build - we have already discussed this option while building the images, so we will not go through it again.

  2. docker ps / docker ps -a - This command lists all running / stopped containers in the workspace

  3. docker rm - this command is used to remove a container from workspace. This is as good as VM termination.

  4. docker rmi - this command is used to remove the underlying image from cache.

  5. Docker images -- ?

Container execution

  1. 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

    1. volume

    2. port

    3. name

    4. attach

    5. rm

    6. The whole list can be found with [docker run --help](https://docs.docker.com/engine/reference/commandline/run/)

  2. docker stop - This will stop the running container.

  3. docker start - This command will start the stopped container.

Container Communication

  1. docker exec - This command will connect to running container and execute a given command on the container.

  2. 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.

Primary goals

Here are a list of things you will learn in this Crio Byte:

  1. Play with containers.

  2. Understand different commands used to manage containers..

  3. Publish, delete, republish and understand the infrastructure .

Open a Linux terminal

Note

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.

Instructions for the Crio Workspace

To open a terminal in the Crio Workspace, follow the steps below:

  1. 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).

  2. Once your workspace is ready, click on Launch online IDE.

  3. Click on View > Open in New Tab option (see image below).

image alt text

  1. Open a new terminal, click on the Menu > View > Terminal. (Note: The menu button is on the top-left (three horizontal bars).

image alt text

Container management

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?

image alt text

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> 

image alt text

Note that this container id is the one picked up from ps.

Now, lets confirm tha the container is running


docker ps 

image alt text

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

image alt text

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

image alt text


docker rm container_id

image alt text


docker ps -a

image alt text

Shows no containers now.

Now let's modify our Dockerfile to remove the contents of apache server

image alt text

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 .

image alt text

Let's try to run it now.

First, try to check is the image is tagged correctly.


docker images

image alt text


docker run -p80:80  apache-server

image alt text

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

image alt text

You will see the default homepage of apache server..

Play with running container

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 

image alt text

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

image alt text

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

image alt text

You can see the terminal opened for bash process. Lets tryt to play around by using ls, cd cat commands

image alt text

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.,

image alt text

Then lets try to create a simple file on our host machine.


echo "This is my simple html file" > ~/index.html

cat ~/index.html

image alt text

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

image alt text

Now lets connect to the docker again to see if the contents are changed.


docker exec -it 958c3c6d7e1e bash

cat htdocs/index.html

image alt text

Does it reflect on the apache server on the container which is attached to port 80?


curl localhost

image alt text

Know more about container management

Now, lets stop this container


docker ps

docker stop upbeat_wescoff

image alt text

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

image alt text

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.

image alt text

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

  1. -p how used 8080 instead of 80, which means guest 80 port is now mapped with 8080 port on the host.

  2. --name my-apache-server - This is the name we are assigning to our container.

  3. -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

image alt text


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

image alt text


docker images -a

Will show a bunch of images which are used and unused.

image alt text

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.

image alt text

Curious Cats

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?

Get introduced to docker orchestration

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

  1. docker build - we have already discussed this option while building the images, so we will not go through it again.

  2. docker ps / docker ps -a - This command lists all running / stopped containers in the workspace

  3. docker rm - this command is used to remove a container from workspace. This is as good as VM termination.

  4. docker rmi - this command is used to remove the underlying image from cache.

  5. Docker images -- ?

Container execution

  1. 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

    1. volume

    2. port

    3. name

    4. attach

    5. rm

    6. The whole list can be found with [docker run --help](https://docs.docker.com/engine/reference/commandline/run/)

  2. docker stop - This will stop the running container.

  3. docker start - This command will start the stopped container.

Container Communication

  1. docker exec - This command will connect to running container and execute a given command on the container.

  2. docker cp - This command is used to copy the files to/from the running container.

We will see these commands in advance docker byte..