Skip to content

Instantly share code, notes, and snippets.

@apanimesh061
Last active May 24, 2018 17:12
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save apanimesh061/7e5291add890560f08fef2e813e67dbc to your computer and use it in GitHub Desktop.
Save apanimesh061/7e5291add890560f08fef2e813e67dbc to your computer and use it in GitHub Desktop.
Creating a commit of a Docker Container

Lets create a docker-machine first. Here I have named my machine docker-ubuntu with 2 GB RAM and 2 CPUs.

$ docker-machine.exe create -d virtualbox --virtualbox-memory "2048" --virtualbox-cpu-count "2" docker-ubuntu
Running pre-create checks...
Creating machine...
(docker-ubuntu) Copying C:\Users\Animesh\.docker\machine\cache\boot2docker.iso to C:\Users\Animesh\.docker\machine\machines\docker-ubuntu\boot2docker.iso...
(docker-ubuntu) Creating VirtualBox VM...
(docker-ubuntu) Creating SSH key...
(docker-ubuntu) Starting the VM...
(docker-ubuntu) Check network to re-create if needed...
(docker-ubuntu) Waiting for an IP...
Waiting for machine to be running, this may take a few minutes...
Detecting operating system of created instance...
Waiting for SSH to be available...
Detecting the provisioner...
Provisioning with boot2docker...
Copying certs to the local machine directory...
Copying certs to the remote machine...
Setting Docker configuration on the remote daemon...
Checking connection to Docker...
Docker is up and running!
To see how to connect your Docker Client to the Docker Engine running on this virtual machine, run: F:\Docker Toolbox\docker-machine.exe env docker-ubuntu

After the docker-machine is created, I need to check the env. variables

$ docker-machine.exe env docker-ubuntu
export DOCKER_TLS_VERIFY="1"
export DOCKER_HOST="tcp://192.168.99.103:2376"
export DOCKER_CERT_PATH="C:\Users\Animesh\.docker\machine\machines\docker-ubuntu"
export DOCKER_MACHINE_NAME="docker-ubuntu"
# Run this command to configure your shell:
# eval $("F:\Docker Toolbox\docker-machine.exe" env docker-ubuntu)

$ eval $("F:\Docker Toolbox\docker-machine.exe" env docker-ubuntu)

Check if there are any containers running

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES

Check if docker-ubuntu machine is running. Since, there is an entry, I meams that the machine was created correctly

$ docker-machine ls
NAME            ACTIVE   DRIVER       STATE     URL                         SWARM   DOCKER    ERRORS
docker-ubuntu   *        virtualbox   Running   tcp://192.168.99.103:2376           v1.12.2

Let us create a contianer now. docker run command automatically creates a new container. Here I am trying to create a container os ubuntu and will log into it's bash as soon as it is created.

NOTE: If the container did not exist before, it will be downloaded.

$ docker run -it ubuntu /bin/bash
Unable to find image 'ubuntu:latest' locally
latest: Pulling from library/ubuntu

6bbedd9b76a4: Pull complete
fc19d60a83f1: Pull complete
de413bb911fd: Pull complete
2879a7ad3144: Pull complete
668604fde02e: Pull complete
Digest: sha256:2d44ae143feeb36f4c898d32ed2ab2dffeb3a573d2d8928646dfc9cb7deb1315
Status: Downloaded newer image for ubuntu:latest
root@0561625910f3:/#

Now I am logged into the shell of the container. Lets make some changes:

root@0561625910f3:/# apt-get update
...
root@0561625910f3:/# apt-get install gcc
....
root@0561625910f3:/# gcc
gcc: fatal error: no input files
compilation terminated.

I will now exit from the bash

root@0561625910f3:/# exit
exit

Now I have a container which has gcc installed in it. I'll run docker ps -a to check if the container was correctly created.

$
$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS                      PORTS               NAMES
0561625910f3        ubuntu              "/bin/bash"         3 minutes ago       Exited (1) 22 seconds ago                       furious_bartik

I want to save the changes that I made to the container, so I'll create an image and same it using docker commit

$ docker commit -a animesh 0561625910f3 ubuntu
sha256:638e4034f68d1067d927de26dc4244233a194b3080bb9c3d6db3bc09f4425adf

I see that the image has been created.

$ docker images
REPOSITORY          TAG                 IMAGE ID            CREATED             SIZE
ubuntu              latest              638e4034f68d        5 seconds ago       264.5 MB
ubuntu              <none>              f753707788c5        32 hours ago        127.2 MB

Let me now stop and restart the container.

$ docker stop 0561625910f3
0561625910f3

$ docker start 0561625910f3
0561625910f3

Now, the container has been restarted

$ docker ps -a
CONTAINER ID        IMAGE               COMMAND             CREATED             STATUS              PORTS               NAMES
0561625910f3        f753707788c5        "/bin/bash"         6 minutes ago       Up 1 seconds                            furious_bartik

Using the following command with the right container ID I am able to log into the correct bash which has gcc installed

$ docker exec -it 0561625910f3 /bin/bash
root@0561625910f3:/# gcc
gcc: fatal error: no input files
compilation terminated.
root@0561625910f3:/#

Now I'll talk about sharing folders:

docker-machine ssh docker-ubuntu
sudo mount -t vboxsf dockershared /home/docker/vm_share
docker run -it --volume /home/docker/vm_share:/home/winshared napa:models "/bin/bash"

Here, dockershared points to a directory on the Host OS. Think of this like Host shares a directory with docker-machine which shares it with an image/container.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment