3.3 Exercise 1 - Docker as a user

Do the following exercise:

  1. alpine image.
    • Search and pull the alpine image (tag 3.12) - it is an official build.
    • Can you run a container from this image and make it print a “hello world” message?
    • Now run a container interactively from the same image.
      • Run whoami in the container.
      • Exit the container and run whoami on the host machine: do you get the same output?
    • Restart the container you just exited:
      • Is it now running?
      • Make the container execute the command ls.
      • Stop the container.
    • Remove the alpine image and all its containers (running or stopped).
Answer

# Search and pull the alpine image (tag 3.12) - it is an official build.
docker search alpine --filter is-official=true
docker pull alpine:3.12

# Can you run a container from this image and make it print a “hello world” message?
docker run alpine:3.12 echo "hello world"

# Now run a container **interactively** from the same image.
docker run -ti alpine:3.12
  # Run `whoami`
  whoami
  # Exit the container.
  exit

# Restart the container you just exited: is it now running?
docker restart CONTAINER_ID # find it with `docker ps -a`

# Make the container execute the command `ls`
docker exec CONTAINER_ID ls

# Stop the container
docker stop CONTAINER_ID

# Remove the alpine image and all its containers (running or stopped)
docker rmi alpine:3.12
docker rm CONTAINER_ID # check all containers with `docker ps -a`
  1. imagemagick
    • Pull the ìmagemagick image that is official and that has the highest number of stars
    • Check the version of the convert command.
    • Start a container interactively.
    • Inside the container: download this png image
    • Convert it to .jpg using the convert command of imagemagick (format; convert image.png image.jpg).
    • Exit the container.
    • Copy the jpg image back from the stopped container! Explore docker cp.
Answer

# Pull image
docker pull acleancoder/imagemagick-full

# Check version of `convert`
docker run acleancoder/imagemagick-full convert --version

# Start interactive container
docker run -it acleancoder/imagemagick-full
  # fetch png image
  > wget https://pbs.twimg.com/profile_images/1273307847103635465/lfVWBmiW_400x400.png
  # convert to jpg
  > convert lfVWBmiW_400x400.png myimage.jpg
  # exit container

# fetch container ID with `ps -a` and use `docker cp` to copy jpg file from the stopped container to the host
docker cp *CONTAINER_ID*:/myimage.jpg .