Some basic docker steps
Run a Ubuntu based docker image:docker run ubuntu
This will run a local image called ubuntu or download one from docker repository.
Actually run an application inside a container. In this case we will run bash:docker run -ti ubuntu:latest bash
This will run the container and open a terminal into it.
While connected to the container, any change that happen will stay in that container and not in the image it self. If we want to create a new image based on that container we can do the following:
Exit the container, by typing exit
or pressing ctrl+d
.
Once we are back to the host command line, we can list the containers with:docker ps -a
This will list all the existing containers. Since we want the last container, we can use -l:docker ps -l
This will list the last exited container:
In order to create an image based on that container, we need to commit it and give a name to the new image. This can be done using the name under the NAMES column and providing a new name for the image:docker commit affectionate_shannon my-ubuntu-2
We can then check our images with docker images
:
Now we can start a new container from that new image with:docker run -it my-ubuntu-2 bash
and any changes done and committed will be there.
Remove Containers
With all the testing you can end up with a couple of container in exited status. These can be removed wi
th:docker rm <container name>
It can take more than one container name per line.
Daemonize a container and attach to it
To run a container in background we need to use the daemon (-d) option:docker run -d -ti my-ubuntu bash
This will start the container in background.
To view the container running, and its name, run:docker ps
In case we need to attach into the container, that can be done with(container name in this example is hundry_payne):docker attach hungry_pane
To detach again from it a key combination is needed:ctrl+p, ctrl+q
To run a new terminal into it we need to use docker exec:docker exec -ti hungry_payne bash