Home Virtualisation Docker Building your first docker container

Building your first docker container

442
0
Reading Time: 3 minutes

Hellooo again, I know so soon right?! In the last blog post, we learnt the basics of creating a docker image, If you missed that post you can check that out here. So as promised in this post we will cover deploying the container locally and “execing” into the container and running some code.

So let’s pick up where we left off last time, We have our alpine image “myalpineimage” with three versions.

docker image list 

So we have a docker image, which is ready to be deployed, We can use the command docker run. for more information about the docker run commands, you can check out the docker run page [Link here]. In the example below we are using the following flags.

docker run [OPTIONS] IMAGE [COMMAND] [ARG...]
FlagsDescription
-iKeep STDIN open even if not attached
-tAllocate a pseudo-TTY
-dRun container in background and print container ID
pwsh or bashcreating an interactive shell session for code execution

Creating a container and connecting into it

So you want to quickly test your container or check something inside of it? If you pass just the -t it will create a container but then close on exit

 docker run -t myalpineimage:0.2 pwsh

While we are connected to the container, If we check the running docker containers we see that it’s active and running

docker ps 

However, as soon as we quit the running instance of “keen_saha” we can see that the container instance has been stopped.

Creating a container without auto-connecting to shell

If you wanted to start the container with a headless deployment and not need to exec into the container you can run the following:

docker run -i -t -d myalpineimage:0.2 pwsh

As we can see the container is running and we’ve not yet exec’d into the container.

Connecting to an existing running container

Finally, If you have a fleet of running containers and you need to connect to a container instance for diagnostics you can run the following commands, Firstly we want to see which containers are running. – In this example, we have 5 containers instances

docker ps

to be able to connect to the container we will want to use the docker exec command – for more information check out the [Docker Docs]

docker exec -i -t hardcore_hugle pwsh

LEAVE A REPLY

Please enter your comment!
Please enter your name here