Learning paths / Containers / Running containers

Try it: run, inspect and stop a container

Reading · 15 min · Module 3, lesson 3 of 420 min left in this module

Module 3 · Running containersLesson 3 of 4

Goal: Run a container with a port and a variable, read its logs, run a command inside one, then stop and remove it.

You need Docker running (set up in lesson 3.1.1), a terminal, and curl.

Check your tools, and a warning on Apple Silicon

docker version should print both a Client and a Server section. If the Server part errors, start Docker Desktop (or the Docker service) and try again.

On an Apple Silicon Mac, step 1 first prints WARNING: The requested image's platform (linux/amd64) does not match the detected host platform. The lab image is built for Intel and AMD processors; Docker runs it through emulation (lesson 3.1.1), and everything below works the same.

If port 3000 is taken, use -p 3001:8080 in step 1 and localhost:3001 in step 2.

  1. Start the sample API

    docker run -d --name hello-api -p 3000:8080 -e GREETING="Hello from a container" quay.io/computesphere/learn-sample-api:1.0.0
    

    The first run downloads the image; later runs start in about a second.

    Docker says the name is already in use?

    Conflict. The container name "/hello-api" is already in use means a container from an earlier try still has the name. Remove it, then run the command again:

    docker rm -f hello-api toolbox moved
    

    No such container for any of the three is fine: it just wasn't there.

    You should seeone long line of letters and digits: the new container's ID.

  2. See it running and call it

    docker ps
    curl localhost:3000/hello
    

    docker ps lists running containers only. PORTS may also show [::]:3000->8080/tcp: the same mapping for IPv6 addresses. The message is the variable you passed with -e.

    You should seea hello-api row with STATUS Up and PORTS 0.0.0.0:3000->8080/tcp, then {"api_key_set":false,"message":"Hello from a container"}

  3. Read its logs

    docker logs hello-api
    

    This app logs one line at start-up and nothing per request, so that's everything it has printed.

    You should seeone line: the date and time, then sample-api 1.0.0 listening on :8080

  4. Try a shell in a distroless container

    docker exec -it hello-api sh
    

    The lab images are distroless: the app and almost nothing else, so there's no shell to open.

    You should seeOCI runtime exec failed ... exec: "sh": executable file not found in $PATH

  5. Open a shell in one that has one

    Start a small Alpine Linux container that sleeps, then open a shell in it:

    docker run -d --name toolbox -e GREETING="Hello from a container" alpine:3.22 sleep 3600
    docker exec -it toolbox sh
    

    At the / # prompt, run:

    echo $GREETING
    ls /
    exit
    

    exit leaves the shell. The container keeps running, because its main program is still sleep.

    You should seea / # prompt, then Hello from a container, and a short list of folders: bin, etc, home, usr and more.

  6. Stop both containers

    docker stop hello-api toolbox
    docker ps -a
    

    -a includes stopped containers. The number in Exited (137) or Exited (2) is the app's exit code: after docker stop, any code is normal. 137 means Docker had to force the program to stop, as it does for sleep.

    Check yourself

    Both containers show Exited. What does docker start hello-api do now?

    You should seeeach name printed back, then both listed with STATUS Exited.

  7. Remove them

    docker rm hello-api toolbox
    docker ps -a
    

    The images stay on your machine for next time. To remove them too, run docker rmi alpine:3.22 quay.io/computesphere/learn-sample-api:1.0.0.

    You should seeboth names printed back, then docker ps -a no longer lists hello-api or toolbox.