Get Started with Docker – Part 1 : Docker Hub, Pulling Images, and Basic Containers Manipulation

undefined

Part 3: Pulling Images from Private Repositories on Docker Hub “Private Images”

Some DevOps Companies, developers teams, and systems administrators create private repositories to hold their private images to use between their teams. To use the private images you must have a Docker Hub ID and login to Docker Hub then pull those private images.

To login to a private repository, first you need to know the docker id and password of the private repository, then run “ docker login ” command as the following:

# docker login
Login with your Docker ID to push and pull images from Docker Hub. If you don't have a Docker ID, head over to https://hub.docker.com to create one.
Username: msemari
Password: 
Login Succeeded

Here’s we successfully logged in to our private repository. Even with login to your private repositories, it’s images will not appear in the search results when using ” docker search ” command , you can get it’s names from Docker Hub web site.

Now, I’ll pull image called Bala7a:0.0, to pull private images it’s name must preceded with the Docker ID “for example msemari/Bala7a:0.0“, run the following command:

# docker pull msemari/Bala7a:0.0

Now, your private image is pulled.

Before moving to the next part, if you want to logout from the private repository, run the following command

# docker logout 
Remove login credentials for https://index.docker.io/v1/

Now, if you try to pull your private images again you will see the following error:

# docker pull msemari/Bala7a:0.0 
Pulling repository docker.io/damlag/chroma
Error: image msemari/Bala7a:0.0 not found

Image not found error is what we got. Now, let’s proceed to the next part.

Part 4: Creating Containers from Images

Locally pulled images are used to create a containers from it, we can create a container from an image using “docker run” command. When you execute a command against an image using docker run, you basically obtain a container. After the command that is executing into container ends, the container stops (you get a non-running or exited container). If you run another command into the same image again a new container is created and so on.

Here’s the syntax and some options used with “docker run

# docker run [OPTIONS] IMAGE [COMMAND] [ARG...]

docker run command will check on the image it uses to create a container from it locally if found it’ll create a container from is, if not found it’ll pull the image locally first then create a container.

Options:

Here’s the most used options you’ll use with docker run command

Option Effect
-d, --detach Run container in background and print container ID
-e, --env value
Set environment variables (default [])
-i, --interactive Keep STDIN open even if not attached
--name string
Assign a name to the container
-p, --publish value
Publish a container’s port(s) to the host (default [])
-t, --tty Allocate a pseudo-TTY
-v, --volume value
Bind mount a volume (default [])

Now, let’s go and create some containers using some of the above options.

Container 1: The easiest way to create a container from an image is by running “docker run <image>” command, Here I’ll create image from CentOS image without running any command against this image. The below example will check on the image CentOS locally if found it’ll create a container from is, if not found it’ll pull the image locally first then create a container then stop, as in the following example:

$ docker run centos

Container 2: The below example will check on the image centos locally if found it’ll create a container from is, if not found it’ll pull the image locally first then create a container. once this container ran it’ll print “Hello MSemari, This is the first unnamed container” on the terminal then stop, as in the following example:

# docker run centos:latest echo "Hello MSemari, This is the first unnamed container"

The container created and printed the message the stopped, here’s the output

undefined

 

Container 3: The container create here uses four options from the above options, first option to interactively connect into a container shell session, and run commands as you do on any other Linux session, second option to enable tty terminal, third option to run the container in the background, and fourth option to give a name to the created container to use instead of the container_ID, this will simplify your work when dealing with containers in the next part, check following example:

# docker run -it -d --name mimastech debian /bin/bash
1bccad2191fa353ae4509157fecc7261005540a80ed3a743346edd4a94738d2f

Here’s our container “mimastech” still running on the background, it’s main process is /bin/bash

 

Container 4: The below example, shows the creation of one of my containers which I use in staging systems. Here, I run the container in background, enable tty and interactive session, naming it to ‘Bala7a0.0_container’, and using two new options -v for mounting “/home/msemari/mount” directory on my host to “/home/deployer/current” directory inside the container, and -p for mapping port “90” on my host to port “3000” inside the container.

# docker run -d -it -v /home/msemari/mount:/home/deployer/current -p 90:3000 --name Bala7a0.0_container msemari/chroma-development:1.0
f347b9bc9c8a921ed08b7df371fd9e644ca2cef4c78090334f171bb9887cc75c

See, we successfully created a staging/production container “Bala7a0.0_container” with some advanced options -v and -p

Before moving to the important part of this article, pay attention to the following hints:

Hints:

1. All the containers created will remain on the host filesystem until you choose to delete them by using the "docker rm" command.
2. You only use docker run once when you create a container, any further work on a container you will use the container commands, explained in the next part.
3. Creating a container with -v option to mount host directory inside a container will keep files on your host even if the container removed "kind of persistent storage".
4. ALWAYS USE OPTIONS -it WITH docker run COMMAND, FOR ME THIS MEANS MUST, AS THESE OPTIONS WILL FACILITATE YOUR WORK ON CONTAINERS.

Now, comes the final and the most important part Dealing with containers.

Part 5: Basic Containers Manipulation

Finally, we here at the most important part of this article Basic Container Manipulation “Dealing with containers”, but Why this part is too important?

Because you’ll use the commands in this part many many many times against all previous commands. You’ll use “docker run" command once to create your development/staging/production containers “sure, you’ll use with “docker run" all options you need your container to have”, but once the container is created you’ll need commands to deal with your containers like “start, stop. restart, list, attach, execute commands, etc,,”. You’ll use these commands periodically. There are many many commands with a lot of options to deal with containers, we’ll only list some of them in this part, other advanced commands will be in our other articles. Let’s start.

  • Listing Containers

You’ll need to list your containers to know the running containers, container’s name, container’s ID, and all containers on your host. You’ll need this info “container’s name and container’s ID for use with other container’s commands.

To list the running containers on your docker host, run the following command:

# docker ps

undefined

As you see, we’ve three running containers, we created two of them from the previous part. From the above command, we knew the container’s name and ID, also the image used for creating each container, container’s up time, etc..

To list all containers on your docker host “running and non-running”, run the following command:

# docker ps -a

undefined

As you see, I’ve 12 containers, three of them are up and 9 are stopped. Actually, I do not need most of them and will remove them in a few seconds.

To list the latest created container on your docker host “either running or non-running”, run the following command:

# docker ps -l

undefined

As seen from the above image, the latest container is a stopped one “on my docker host”. Also latest created container is the first one appears when running “docker ps -a ” command. In The next page you’ll know more about the basic container manipulations.

If You Appreciate What We Do Here On Mimastech, You Should Consider:

  1. Stay Connected to: Facebook | Twitter | Google+
  2. Support us via PayPal Donation
  3. Subscribe to our email newsletters.
  4. Tell other sysadmins / friends about Us - Share and Like our posts and services

We are thankful for your never ending support.

Leave a Reply

Your email address will not be published. Required fields are marked *