Showing posts with label Docker. Show all posts
Showing posts with label Docker. Show all posts

Steps to create docker containers in your laptop

In this post we will see how to create a docker container of ubuntu on your windows laptop.


I am using “Docker for Windows” software to run dockers on my Windows 10 laptop. You can get “Docker for Windows” by clicking on this link .
If you have Windows 7 download Docker Toolbox for Windows with Virtualbox.

Ubuntu docker creation
Once you are done with docker installation let’s move ahead.
  • In the windows command prompt or in “Docker Quickstart Terminal” execute below command. By default it will pull the latest image of ubuntu container available in repository.
C:\CloudVedas>docker run ubuntu
  • If you need  specific version of Ubuntu you can mention the version name in command. Like below we are pulling Ubuntu 14.04 version. You can check all the available versions here .
C:\CloudVedas> docker run ubuntu:14.04
Unable to find image 'ubuntu:14.04' locally
14.04: Pulling from library/ubuntu
2e6e20c8e2e6: Pulling fs layer
30bb187ac3fc: Pulling fs layer

  • Now let’s see the image we have downloaded .
C:\CloudVedas> docker image ls
REPOSITORY      TAG                 IMAGE ID            CREATED             SIZE
ubuntu                   14.04               6e4f1fe62ff1        4 months ago        197MB

  • Let’s create a container with that image using the image id. Here we are using -t and -d option so that the container keeps on running in detached mode and we can login to it.
C:\CloudVedas>docker run -t -d 6e4f1fe62ff1

  • List the running containers using “docker container ls”
C:\CloudVedas>docker container ls
a8dee68d7802        6e4f1fe62ff1   "/bin/bash"      About a minute ago   Up About a minute eager_johnson

We can see our container is created 5 minutes ago and is up. You can also identify the container using the container id. Note that the container ID is same as the first 12 digits of the string we got when we executed docker run in last step.
  • Let’s get inside our container and check it.
C:\CloudVedas>docker exec -it  a8dee68d7802 /bin/bash

Once you are inside the Ubuntu container you can explore it. Let’s check the OS version.
root@a8dee68d7802:/# more /etc/os-release NAME="Ubuntu" VERSION="14.04.6 LTS, Trusty Tahr" ID=ubuntu ID_LIKE=debian PRETTY_NAME="Ubuntu 14.04.6 LTS" VERSION_ID="14.04" HOME_URL="" SUPPORT_URL="" BUG_REPORT_URL="" root@a8dee68d7802:/#
  • If you don’t see your container up, it may have stopped automatically. To check all the containers which are/were running, execute command
C:\CloudVedas>docker ps -a

You can also create your own customized container image which you can use to deploy more containers in your environment. Refer this post on how to create a container image.  If instead of Docker hub you are using AWS ECR, check the post on how to push/pull image to/from AWS ECR

If you want to try next level on docker you can user dockerfile or docker-compose files to install your application. Like in our other post we cover how you can install wordpress in docker.

Hope this post helped you. Do let us know if you have any query or you get stuck in any installation step. 

Solved: How to cap or limit memory usage of a docker container

In this post we will see how we can cap or restrict the maximum amount of memory the container can use.
Let’s first see the current usage of container id ec6ed4af7c34 with “docker stats”.
docker stats ec6ed4af7c34
In the below image we can see the current limit of the container is 300MiB

Now let’s change this limit to 200MiB of a running container.
docker container update -m 200m ec6ed4af7c34
Now when we look at “docker stats” we can see in the image below the new limit on the container.

If you want to set the memory limit at the time of launching the container itself do it as
docker run -exec -it -m 200m image-name /bin/bash
Compose file version 3
If you want to restrict the usage from the compose file itself you can follow below example, the redis service is constrained to use no more than 50M of memory and 0.50 (50%) of available processing time (CPU), and has 20M of memory and 0.25 CPU time reserved (as always available to it).

version: '3'
    image: redis:alpine
          cpus: '0.50'
          memory: 50M
          cpus: '0.10'
          memory: 20M

Hope this post is helpful to you. Do let me know if you have any query.

AWS ECR : How to push or pull docker image

Hello everyone!
In this post we will see how to push a docker image to your AWS ECR  and how to pull image from it.
  • Skip this step if you already have docker on your machine. I am using  “Docker for Windows” software to run dockers on my Windows 10 laptop.
If you have Windows 7 download Docker Toolbox for Windows with Virtualbox.
  • Get AWS CLI.
  • Create AWS IAM user from AWS console which has permission to put and delete images. You can refer sample policy below.
    "Version": "2012-10-17",
    "Statement": [
            "Sid": "VisualEditor0",
            "Effect": "Allow",
            "Action": "ecr:*",
            "Resource": "*"
Once you are done with pre-requisites let's move forward.
1)  Open powershell in windows or command prompt in linux. Below I'll be running command on windows powershell. But the AWS CLI command on linux are similar.
In powershell check that you have docker running. It should give you an output like below.
PS C:\CloudVedas> docker ps -a


55f016be65aa hello-world "/hello" 2 hours ago Exited (0) 2 hours ago gifted_hamilton

PS C:\CloudVedas>
2) Configure AWS CLI by entering the access key and secret key of the IAM user.
PS C:\CloudVedas> aws configure
AWS Access Key ID [****************A37B]:
AWS Secret Access Key [****************W3w3]:
Default region name [ap-southeast-2]:
Default output format [None]:
PS C:\CloudVedas>
3) Check if your IAM user is able to describe ECR.
PS C:\CloudVedas> aws ecr describe-repositories
    "repositories": []
PS C:\CloudVedas>
4) Let's create an ECR repository now. You can skip this step if you already have repo.
PS C:\CloudVedas> aws ecr create-repository --repository-name cloudvedas
"repository": {
"repositoryArn": "arn:aws:ecr:ap-southeast-2:123456789123:repository/cloudvedas",
"registryId": "123456789123",
"repositoryName": "cloudvedas",
"repositoryUri": "",
"createdAt": 1564224171.0
PS C:\CloudVedas>
5) Next we will authenticate the Docker client to the Amazon ECR registry to which we intend to push our image. You will get a long docker login token as below.
PS C:\CloudVedas> aws ecr get-login --region ap-southeast-2
docker login -u AWS -p eyJxxxxxxxxxxxx094YwODF9 \ 
-e none
6) Resulting output is a docker login command. After stripping the "-e none" copy and paste the docker login command in your terminal.
PS C:\CloudVedas> docker login -u AWS -p eyJxxxxxxxxxxxxx094YwODF9  \
Login Succeeded
If you don't strip -e none you will get below error.
unknown shorthand flag: 'e' in -e
See 'docker login --help'.
You will see "Login Succeeded" message once you are logged in successfully. Continue to Step 7 if you want to push image. Skip to step 10 if you want to pull image from ECR.
Push Image
7) Tag your image with the Amazon ECR registry, repository, and optional image tag name combination to use.
PS C:\CloudVedas> docker images
hello-world latest fce289e99eb9 6 months ago 1.84kB
PS C:\CloudVedas>

PS C:\CloudVedas> docker tag fce289e99eb9

PS C:\CloudVedas> docker images
REPOSITORY TAG IMAGE ID CREATED SIZE latest fce289e99eb9 6 months ago 1.84kB
hello-world latest fce289e99eb9 6 months ago 1.84kB
PS C:\CloudVedas>
8) Next let's push the image.
PS C:\CloudVedas> docker push
The push refers to repository []
af0b15c8625b: Pushed
latest: digest: sha256:92c7f9c92844bb49837dur49vnbvm7c2a7949e40f8ea90c8b3bc396879d95e899a size: 524
PS C:\CloudVedas>
9) We just now pushed the image. Let's check our image in ECR.
PS C:\CloudVedas> aws ecr describe-images --repository-name cloudvedas
"imageDetails": [
"registryId": "123456789123",
"repositoryName": "cloudvedas",
"imageDigest": "sha256:92c7f9c92844bb49837dur49vnbvm7c2a7949e40f8ea90c8b3bc396879d95e899a",
"imageTags": [
"imageSizeInBytes": 2487,
"imagePushedAt": 1564224404.0
PS C:\CloudVedas>
Great ! We can see our image in ECR and it has a tag "latest".
Pull Image
10) If you want to pull the image you have to follow same instruction till step 6, after that just execute below command.
PS C:\CloudVedas> docker pull

Solved: docker - error during connect

sagu@sagu-pc MINGW64 ~$ docker image lserror during connect: Get http://%2F%2F.%2Fpipe%2Fdocker_engine/v1.29/images/json: open //./pipe/docker_engine: The system cannot find the file specified. In the default daemon configuration on Windows, the docker client must be run elevated to connect. This error may also indicate that the docker daemon is not running.sagu@sagu-pc MINGW64 ~
If you are getting above error while running the docker commands in Docker Quickstart Terminal then you can have one of the listed problem.
  • The docker deamon is not running.
  • The docker host machine itself is down.
  • Required configuration file is missing.
First check that the host machine is up and the configuration file is present at its location. Once you are sure both the above things are ok, in that case easiest way to get rid of this issue is to open the Docker Quickstart Terminal by selecting “Run as Administrator” .  It will take some time to start but let it complete and you should be back in business soon.

Solved: How to resize Docker Quickstart Terminal Window

By default in Windows 7 Docker Quickstart Terminal Window size will be small. But it can be very annoying to work in such small window.
Here we will show you how to increase the window size as per your requirement.
  • Open the Docker Quickstart Terminal as an Administrator.
  • Right Click on the Blue whale icon on top of  Docker Quickstart Terminal .
  • Click “Properties” and Select “Layout” tab.
  • Increase the “Width” and “Height” of “Window Size” as per your requirement.
  • Finally Click OK and try re-opening the Terminal.
That’s all folks!

Solved: How to restart a docker container automatically on crash

In this post we will see how we can restart a container automatically if it crashes.
If you want a Docker container to always restart use:-
docker run -dit --name cldvds-always-restart --restart=always busybox
But if you want container to always restart unless it is explicitly stopped  or restarted, use:-
docker run -dit --name cldvds-except-stop --restart unless-stopped busybox
In case you want the container to stop after 3 restart attempt use below command.
docker run -dit --name cldvds-restart-3 --restart=on-failure:3 busybox
You can see the logs of a container using
docker logs cldvds-restart-3
If you want to change the restart policy of running container you can do it with “docker update” e.g. here we are changing restart attempt from 3 to 4 of container cldvds-restart-3.
docker update --restart=on-failure:4 cldvds-restart-3

Solved: How to copy paste in Docker Quickstart Terminal

If you want to copy/paste the contents on Docker Quickstart Terminal using mouse follow these steps.
  • Open the Docker Quickstart Terminal as an Administrator.
  • At the top of terminal right Click on the Blue Whale icon and select “Defaults”.
  • In the “Options” tab of new window check the QuickEdit Mode and click OK.
  • Now with mouse left click you can select the content and paste with right click.

Solved: How to create an image of a docker container.

So you have completed all the installation on a docker container and now you want to keep it as golden image.
Golden images are useful when you want to create more containers with same configuration. This also ensure that when you ship an image from Dev to UAT or Prod it will be exactly same as it was when you tested it.
This also avoid problems during release.
So how you create an image from a running container.
Let’s have a look. We have a running container with ID d885f4ea3cff.
docker container ls
d885f4ea3cff d355ed3537e9 "/bin/bash" 25 hours ago Up 53 minutes ansible
We already did all the installations in it.  So we will commit and create an image “ansible-base” .
docker commit d885f4ea3cff ansible-base
Now if you look at the image list you should see the new image.
docker image ls
ansible-base latest 1fbdr6f81e4c 39 minutes ago 159 MB
You can create a new container with this image
docker run -t -i -d 1fbdr6f81e4c /bin/bash
Notice the “-d” above it will run the container in detached mode so even if you logout of the container it will remain up.
List the containers that are running
docker container ls
Login to the container with container id d985be2f2c3e you created now.
 docker exec -it d985be2f2c3e bash
If you want to rename the new container check this post .

Solved: Error when allocating new name - Docker

Error response from daemon: Error when allocating new name: Conflict. The container name "/webserver" is already in use by container 6c34a8wetwyetwy7463462d329c9601812tywetdyud76767d65f7dc7ea58d8541. You have to remove (or rename) that container to be able to reuse that name.
If you see the above error it is because a container with same name exist.
Let’s check our running containers
docker container ls
If you don’t see any running container with that name, check the stopped containers.
docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb45bd14c8987 1fbd5d581e4c "/bin/bash" 2 minutes ago Up 2 minutes competent_keller59b9f5a63ba0 ansible-base "/bin/base" 3 minutes ago Created wizardly_payne6c34a8a6edb6 d355ed3537e9 "/bin/bash" 9 minutes ago Exited (0) 4 minutes ago webserver
Above we can see that we already have a container with name webserver.
So we will rename the old container.
docker rename webserver webserver_old
Now if we check again. Our container is renamed to webserver_old .
docker ps -aCONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMESb45bd14c8987 1fbd5d581e4c "/bin/bash" 4 minutes ago Up 4 minutes competent_keller59b9f5a63ba0 ansible-base "/bin/base" 5 minutes ago Created wizardly_payne6c34a8a6edb6 d355ed3537e9 "/bin/bash" 10 minutes ago Exited (0) 5 minutes ago webserver_old
And if you don’t need the old container you can also delete it to free up the space.
docker rm webserver_old
Now if you try to create a container with “webserver” name you should not get any error.

Solved: Error while deleting docker network

When you try to delete a network you may get the below error.
C:\>docker network rm network_name
Eresponse from daemon: network network_name has active endpoints
This error comes when you have an active endpoint. First you have to inspect the network to check if any container is still running which is using the network.
docker inspect network_name
Check running containers using
docker container ls
If you find any container running which is using the network that you are trying to delete, you will have to first stop and remove that container. Be sure that you don’t need that container.
docker stop container_name
docker rm container_name
If you don’t see any container running with the name given in the inspect output. Than it means that the container that you deleted earlier was not removed properly and it has traces remaining in network.
To remove the endpoint from network run this command.
docker network disconnect --force network_name container_name
Finally you should be able to remove the network now.
docker network rm network_name

How to load database in mysql docker container?

After creating the mysql docker container i wanted to load a new database dump to it.
In case you are wondering how to create dockers on you windows machine you can refer my post here .
If you are just testing it you can download a sample mysql database from here .
Once you have downloaded the sample DB unzip it in a folder.
First, copy the database into the container:
$ docker cp mydump.sql c598nvcvc190:/root  # Here c598nvcvc190 is name of database container
Second, Connect to your docker container:
$ docker exec -it c598nvcvc190 /bin/bash
Finally, restore the database dump file into your database “After creating it first”:
# mysql -uroot -prootpassword < /root/mydump.sql
Now you should be able to see the new database listed in mysql.
Be Sociable. Share It. Happy Learning!