# Docker Networking - A Quick Guide to Get You Started

By Bobby Iliev, 2025-01-31. https://bobbyiliev.com/post/docker-networking-a-quick-guide-to-get-you-started/

In this post, I'll show you how Docker networking works in simple terms. If you've ever wondered how containers talk to each other or connect to the internet, this guide is for you!

If you want to try this out but don't have a server, you can use my DigitalOcean referral link to get a free $200 credit: [Free $200 Credit For DigitalOcean](https://m.do.co/c/2a9bba940f39). This is more than enough to spin up a few Droplets and practice Docker networking!

And if you're new to Docker, I've written a free comprehensive Docker ebook that covers all the basics - you can check it out at [Introduction to Docker eBook](https://github.com/bobbyiliev/introduction-to-docker-ebook). The ebook will help you get started with Docker before diving into networking!

## Docker Network Types

Docker comes with 5 network types out of the box:

1. Bridge (default)
2. Host
3. None
4. Overlay
5. MacVlan

Check out the documentation for the network drivers [here](https://docs.docker.com/engine/network/drivers/).

Let's look at each type and see how they work in real-world scenarios!

## Bridge Network - The Default Choice

When you install Docker, it creates a default bridge network automatically. All containers use this network unless you tell them otherwise. Think of a bridge network like a virtual switch - it lets containers on the same network talk to each other.

Let's first check what networks we already have. Run this command:

```bash
docker network ls
```

You'll see something like this:
```
NETWORK ID     NAME      DRIVER    SCOPE
abc123def456   bridge    bridge    local
xyz789uvw321   host      host      local
123abc456def   none      null      local
```

### Creating Your Own Bridge Network

While the default bridge network works, creating your own is better for a few reasons:
- Your containers can find each other by name (like 'laravel-app' or 'mysql-db')
- You get better isolation from other containers on your system
- You can connect and disconnect containers while they're running

Here's how to create your own bridge network. We'll call it 'my-network':

```bash
docker network create my-network
```

Now let's try something practical - we'll run an Nginx container and a test container to see if they can talk to each other:

```bash
# Start a Nginx container
docker run -d --name web --network my-network nginx

# Start an Ubuntu container
docker run -it --name ubuntu --network my-network ubuntu bash
```

From inside the Ubuntu container, you can now ping the Nginx container by name. This is super useful when setting up connections between your app and database:

```bash
ping web
```

## Host Network - Direct Access

The host network is different - it removes the network isolation between the container and your Docker host. It's like running the program directly on your machine. This can be useful for specific cases where you need the best network performance.

Here's how to use host networking:

```bash
docker run -d --network host nginx
```

Now Nginx is available directly on port 80 of your host - you don't need to map any ports!

> Warning: Be careful with host networking. Only use it when you really need to because it gives containers full access to your host's network.

## Port Mapping

When you're using bridge networking (which is most of the time), you need to map container ports to host ports if you want to access them from outside. Let me show you how this works:

```bash
docker run -d -p 8080:80 nginx
```

This command tells Docker: "Take port 80 inside the container and make it available as port 8080 on my host machine." Now you can access Nginx at `http://localhost:8080`

## Real World Example: Running Laravel with MySQL

Let's set up a more realistic example - a Laravel application with MySQL. We'll create a network for them to communicate and set up everything properly.

First, let's create a network for our Laravel app:

```bash
# Create a network for our Laravel application
docker network create laravel-net
```

Now, let's start MySQL. We'll give it a specific name so our Laravel app can find it:

```bash
# Start MySQL with specific settings for Laravel
docker run -d \
    --name mysql-db \
    --network laravel-net \
    -e MYSQL_ROOT_PASSWORD=secret \
    -e MYSQL_DATABASE=laravel \
    -e MYSQL_USER=laravel \
    -e MYSQL_PASSWORD=secret \
    mysql:5.7
```

Let's break down what this does:
- Creates a container named 'mysql-db'
- Connects it to our 'laravel-net' network
- Sets up a root password
- Creates a database called 'laravel'
- Creates a user 'laravel' with password 'secret'

Now for our Laravel application:

```bash
# Start the Laravel application
docker run -d \
    --name laravel-app \
    --network laravel-net \
    -p 8000:80 \
    -e DB_HOST=mysql-db \
    -e DB_DATABASE=laravel \
    -e DB_USERNAME=laravel \
    -e DB_PASSWORD=secret \
    your-laravel-image
```

Here's what's happening:
- The app is named 'laravel-app'
- It's connected to the same network as MySQL
- Port 80 in the container is mapped to 8000 on your host
- Environment variables are set to connect to MySQL
- Notice we use 'mysql-db' as the database host - that's the container name!

## Checking Container Networks

Here are some helpful commands to check your container networks. I use these all the time when debugging:

1. Want to see what network a container is using? Try this:
```bash
docker inspect -f '{{range $key, $value := .NetworkSettings.Networks}}{{$key}} {{end}}' laravel-app
```

2. Need a list of all your networks?
```bash
docker network ls
```

3. Want detailed info about a network? This shows all containers connected to it:
```bash
docker network inspect laravel-net
```

## Troubleshooting Tips

Here are some common issues and how to fix them:

1. If your Laravel app can't connect to MySQL:
   - Check if both containers are on the same network
   - Try `docker exec laravel-app ping mysql-db`
   - Look at the environment variables in your Laravel app

2. If you can't access your Laravel app from your browser:
   - Check if the ports are mapped correctly with `docker ps`
   - Make sure no other service is using port 8000 on your host
   - Try `curl localhost:8000` from your host machine

3. Network seems slow?
   - Consider using host networking for development
   - Check container logs for any error messages
   - Monitor container resources with `docker stats`

## Conclusion

That's the basics of Docker networking! Remember these key points:
- Use bridge networks for connecting your containers
- Create your own networks instead of using the default bridge
- Use container names as hostnames when containers need to talk
- Map ports when you need to access containers from outside

If ports, subnets and DNS are still fuzzy, the free [Networking fundamentals guide](https://devops-daily.com/guides/networking-fundamentals) on DevOps Daily explains the parts Docker builds on.

Now you can set up proper networking for your Laravel applications in Docker. Try creating different networks and connecting containers to see how it all works together.

Have questions? You can reach me on Twitter [@bobbyiliev_](https://twitter.com/bobbyiliev_) or check out my blog at [bobbyiliev.com](https://bobbyiliev.com)!
