Docker: Difference between revisions

From Wiki
Jump to navigation Jump to search
Created page with " == Managing Containers == <pre> docker run -i -t ubuntu /bin/bash # create a container docker run --name bob_the_container \ -i -t ubuntu /bin/bash # create a containe..."
 
Line 4: Line 4:
<pre>
<pre>
docker run -i -t ubuntu /bin/bash  # create a container
docker run -i -t ubuntu /bin/bash  # create a container
docker run --name bob_the_container \
docker run --name bob_the_container -i -t ubuntu /bin/bash  # create a container with specified name
    -i -t ubuntu /bin/bash  # create a container with specified name


docker ps -a  # show all containers
docker ps -a  # show all containers
Line 13: Line 12:
                                 # NOTE: hit Enter to get a prompt
                                 # NOTE: hit Enter to get a prompt


docker run --name daemon_dave -d \ # create daemonized container
# create daemonized container
    ubuntu /bin/sh -c \
docker run --name daemon_dave -d ubuntu /bin/sh -c \
     "while true; do echo hello world; sleep 1; done"
     "while true; do echo hello world; sleep 1; done"


Line 34: Line 33:
     /bin/sh -c "while true; do echo hello world; sleep 1; done"
     /bin/sh -c "while true; do echo hello world; sleep 1; done"
# might be better to use this flag
# might be better to use this flag
--restart=on-failure:5  # max of 5 restarts
    --restart=on-failure:5  # max of 5 restarts


docker inspect daemon_dave  # get container details
docker inspect daemon_dave  # get container details


# get IP address
# get IP address
docker inspect --format '{{ .NetworkSettings.IPAddress }}' \
docker inspect --format '{{ .NetworkSettings.IPAddress }}' daemon_dave
    daemon_dave


docker rm 80430f8d0921  # delete a stopped container
docker rm 80430f8d0921  # delete a stopped container
</pre>
</pre>

Revision as of 19:07, 16 December 2014

Managing Containers

docker run -i -t ubuntu /bin/bash  # create a container
docker run --name bob_the_container -i -t ubuntu /bin/bash  # create a container with specified name

docker ps -a  # show all containers

docker start bob_the_container  # start a stopped container
docker attach bob_the_container  # attach to a container 
                                 # NOTE: hit Enter to get a prompt

# create daemonized container
docker run --name daemon_dave -d ubuntu /bin/sh -c \
    "while true; do echo hello world; sleep 1; done"

# view terminal output inside the container
docker logs daemon_dave     
docker logs -f daemon_dave  # like tail -f
docker logs -t daemon_dave  # print timestamps

docker top daemon_dave  # see what's happening inside the container

# run a process inside the container
docker exec -d daemon_dave touch /etc/config_file  # background
docker exec -t -i daemon_dave /bin/bash  # interactive

docker stop daemon_dave  # stop a daemonized container

# automatic container restarts
docker run --restart=always --name daemon_dave -d ubuntu \
    /bin/sh -c "while true; do echo hello world; sleep 1; done"
# might be better to use this flag
    --restart=on-failure:5   # max of 5 restarts

docker inspect daemon_dave  # get container details

# get IP address
docker inspect --format '{{ .NetworkSettings.IPAddress }}' daemon_dave

docker rm 80430f8d0921  # delete a stopped container