Tuesday, December 19, 2017

docker create vs docker run

This exercise takes a base image jboss/wildfly, spins a container, add a ping.war in the deployments folder and commits a new image named wildflywithping. You can easily reproduce yourself, all you need is docker and a pair of hands (one is enough... also no hands but a pen in your mouth can be enough)


#this pulls a new image from docker hub to local registry
docker pull jboss/wildfly
docker images

REPOSITORY TAG IMAGE ID CREATED SIZE
jboss/wildfly latest ec52433b28ee 2 weeks ago 622MB


#this creates a container from imageid
docker create ec52433b28ee
docker ps -a

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
177bdb401283 ec52433b28ee "/opt/jboss/wildfly/…" 4 minutes ago Created dreamy_lamarr


but the container is not running:

docker exec -ti 177bdb401283 /bin/bash

Error response from daemon: Container 177bdb4012832d42a386ad24c92588d4d245b27249fb95e146d98f5266a74706 is not running


docker start 177bdb401283
docker exec -ti 177bdb401283 /bin/bash

[jboss@177bdb401283 ~]$


in another terminal, run this

docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
177bdb401283 ec52433b28ee "/opt/jboss/wildfly/…" 8 minutes ago Up About a minute 8080/tcp dreamy_lamarr




#rename container
docker rename 177bdb401283 con_pvwildfly
docker ps

CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES
177bdb401283 ec52433b28ee "/opt/jboss/wildfly/…" 22 minutes ago Up 15 minutes 8080/tcp con_pvwildfly


#now you can use container name instead of containerid
docker exec -ti con_pvwildfly /bin/bash


One could have reached the same result in a single command:

docker run --name con_pvwildfly -ti jboss/wildfly /bin/bash


Now, in the container, go to the wildfly deployment folder, we shall copy here a ping.war from outside:

cd /opt/jboss/wildfly/standalone/deployments
ls -ltra

Open a new terminal on the host (not in the container!):

curl -O https://github.com/AdamBien/ping/releases/download/ping-0.0.1/ping.war
(see https://github.com/AdamBien/ping)
docker cp ping.war con_pvwildfly:/opt/jboss/wildfly/standalone/deployments

and check in the container that the file was copied: ls -ltra

docker stop con_pvwildfly

docker commit con_pvwildfly wildflywithping
sha256:99d6ae628596f0fa658f4927bfab1823d654d81bf4afd6b004edfede39ce34cd

docker images

REPOSITORY TAG IMAGE ID CREATED SIZE
wildflywithping latest 99d6ae628596 33 seconds ago 622MB






No comments: